<?php
namespace Home\Model;
use Think\Model;
class ProductModel extends Model{
	public function getProducts($data){
		$page=array(
			'start'		=>	$data['start']?$data['start']:0,
			'limit'		=>	$data['limit']?$data['limit']:20
			);
		$map=array(
			'language_id'	=>	get_default_lng_id(),
			'status'		=>	1
			);
		$order='sort_order '. ($data['order']?$data['order']:'ASC');
		if(isset($data['category_id'])) $map['category_id']=array('in',$data['category_id']);
		if($data['title']) $map['title'] = array('like',"%".$data['title']."%");
		$products=$this->join('LEFT JOIN  __PRODUCT_DESCRIPTION__ ON __PRODUCT__.product_id=__PRODUCT_DESCRIPTION__.product_id')->where($map)->field(C('DB_PREFIX')."product.product_id,title,category_id,image,status,meta_description")->limit($page['start'],$page['limit'])->order($order)->select();
		foreach ($products as $k => $v) {
			$products[$k]['url']=U('Product/detail?product_id='.$v['product_id']);
		}
		return $products;
	}

	public function getCount($data){
		$map=array(
			'language_id'	=>	get_default_lng_id(),
			'status'		=>	1
			);
		$order='sort_order '. ($data['order']?$data['order']:'ASC');
		if(isset($data['category_id'])) $map['category_id']=array('in',$data['category_id']);
		if($data['title']) $map['title'] = array('like',"%".$data['title']."%");

		$count=$this->join('LEFT JOIN  __PRODUCT_DESCRIPTION__ ON __PRODUCT__.product_id=__PRODUCT_DESCRIPTION__.product_id')->where($map)->field(C('DB_PREFIX')."product.product_id,title,category_id,image,status")->order($order)->count();
		return $count;
	}

	public function getSomeProducts($data=array()){
		$map=array(
			'language_id'	=>	get_default_lng_id(),
			'status'		=>	1,
			C('DB_PREFIX') . 'product.product_id'=>array('IN',$data)
			);
		$order='sort_order ASC,product_id DESC';
		$products=$this->join('LEFT JOIN  __PRODUCT_DESCRIPTION__ ON __PRODUCT__.product_id=__PRODUCT_DESCRIPTION__.product_id')->where($map)->field(C('DB_PREFIX')."product.product_id,title,category_id,image,status,meta_description")->order($order)->select();
		foreach ($products as $k => $v) {
			$products[$k]['url']=U('Product/detail?product_id='.$v['product_id']);
		}
		return $products;
	}

	public function getProduct($product_id){
		$map=array(
			'status'=>1,
			'product_id'=>$product_id,
			);
		$product=$this->where($map)->find();
		if($product){
			$map=array(
				'language_id'=>get_default_lng_id(),
				'product_id'=>$product_id
				);
			$product['product_description']=M('product_description')->where($map)->find();

			$product['product_image']=$this->getProductImage($product_id);
			$product['product_attribute']=$this->getProductAttribute($product_id);
			return $product;
		}else{
			return false;
		}

	}
	private function getProductImage($product_id){
		$images=M('ProductImage')->where(array('product_id'=>$product_id))->order('sort_order ASC')->select();
		return $images;
	}

	private function getProductAttribute($product_id){
		$language_id=get_default_lng_id();
		$return=array();
		$Db=M();
		$product_attribute_group=$Db->query("SELECT ag.attribute_group_id, agd.name FROM " . C('DB_PREFIX') . "product_attribute pa LEFT JOIN " . C('DB_PREFIX') . "attribute a ON (pa.attribute_id = a.attribute_id) LEFT JOIN " . C('DB_PREFIX') . "attribute_group ag ON (a.attribute_group_id = ag.attribute_group_id) LEFT JOIN " . C('DB_PREFIX') . "attribute_group_description agd ON (ag.attribute_group_id = agd.attribute_group_id) WHERE pa.product_id = '" . (int)$product_id . "' AND agd.language_id = '" . (int)$language_id . "' GROUP BY ag.attribute_group_id ORDER BY ag.sort_order, agd.name");
		foreach ($product_attribute_group as $v) {
			$product_attribute_data=array();
			$product_attribute=$Db->query("SELECT a.attribute_id, ad.name, pa.text FROM " . C('DB_PREFIX') . "product_attribute pa LEFT JOIN " . C('DB_PREFIX') . "attribute a ON (pa.attribute_id = a.attribute_id) LEFT JOIN " . C('DB_PREFIX') . "attribute_description ad ON (a.attribute_id = ad.attribute_id) WHERE pa.product_id = '" . (int)$product_id . "' AND a.attribute_group_id = '" . (int)$v['attribute_group_id'] . "' AND ad.language_id = '" . (int)$language_id . "' AND pa.language_id = '" . (int)$language_id . "' ORDER BY a.sort_order, ad.name");
			foreach($product_attribute as $value){
				$product_attribute_data[] = array(
					'attribute_id' => $value['attribute_id'],
					'name'         => $value['name'],
					'text'         => $value['text']
				);

			}
			$return[] = array(
				'attribute_group_id' => $v['attribute_group_id'],
				'name'               => $v['name'],
				'attribute'          => $product_attribute_data
			);
		}
		return $return;
	}


}